package com.amoby.main.action.cbabill;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amoby.base.common.BaseAction;
import com.amoby.base.common.Pager;
import com.amoby.main.entity.Cbaacc;
import com.amoby.main.entity.SysUsers;
import com.amoby.main.service.CbaMgrService;

/**
 * @date 2015-7-6 下午3:12:31
 * @author zhaoyy
 * @version V1.0
 * @Description 付款账户管理
 * 
 */
@Controller
public class CbaAccAction extends BaseAction {
    private static final Logger logger = LoggerFactory.getLogger(CbaAccAction.class);
    @Autowired
    private CbaMgrService       cbaMgrService;

    /**
     * 初始化页面
     * @author Lasing 2016-5-27 下午4:21:00
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/menus/cbaacc_mgr.html", method = RequestMethod.GET)
    public String indexPage(HttpServletRequest request, ModelMap model) {
        return AD_HTML + "cbabill/cbaacc_mgr";
    }
    /**
     * 分页查询付款账户
     * @author Lasing 2016-5-27 下午4:26:28
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/cbaacc/list.html")
    public Object dataGridList(HttpServletRequest request, ModelMap model) throws Exception {
        Pager pager = createPager(request);
        pager.addParam("bsb", getRequestParams(String.class, request, "bsb"));
        pager.addParam("acc", getRequestParams(String.class, request, "acc"));
        pager.addParam("accName", getRequestParams(String.class, request, "accName"));
        pager.addParam("use", getRequestParams(Integer.class, request, "use"));
        pager.addParam("enable", getRequestParams(Integer.class, request, "enable"));
        
        cbaMgrService.loadCbaAccPage(pager);
        return getGridData(pager);
    }

    /**
     * 保存付款账户
     * @author Lasing 2016-5-27 下午4:26:55
     * @param item
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/cbaacc/save.html", method = RequestMethod.POST)
    public Object saveItem(Cbaacc item, HttpServletRequest request, ModelMap model) throws Exception {
        Map<String, Object> result = getResultMap();
        try {
            SysUsers user = getUser();
            item = cbaMgrService.saveCbaAcc(item, user);
            result.put(RESULT, true);
            result.put(MESSAGE, "保存成功");
        } catch (IllegalArgumentException e) {
            logger.error("操作失败，参数有误", e);
            result.put(RESULT, false);
            result.put(MESSAGE, "操作失败，输入参数有误！");
        } catch (DuplicateKeyException e) {
            logger.error("保存失败， 数据重复", e);
            result.put(RESULT, false);
            result.put(MESSAGE, "保存失败， 数据已存在！");
        } catch (Exception e) {
            logger.error("保存失败", e);
            result.put(RESULT, false);
            result.put(MESSAGE, "保存失败");
            result.put("exception", e.getMessage());
        }
        return result;
    }
}
